﻿using EntityPet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using PET.Tools;

namespace PET.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class adoptController : ControllerBase
    {
        private readonly PetDbContext db;
        public adoptController(PetDbContext _context)
        {
            db = _context;

        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Result Get()
        {
            Result result = new Result();
            var resultData = db.Pet_adopt.ToList<Pet_adopt>();
            //result.Data = resultData;
            //result.Code = 200;
            //result.Name = "1598033581@qq.com";
            return result;
        }

        /// <summary>
        /// 用id查询数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        public Result Get(Guid Id)
        {
            Result result = new Result();
            Pet_adopt? resulrData = db.Pet_adopt.Where(w => w.ID == Id).FirstOrDefault<Pet_adopt>();
            //if (resulrData == null)
            //{
            //    result.Code = 404;
            //}
            //else
            //{
            //    result.Code = 200;
            //    result.Data = resulrData;
            //}
            return result;
        }

        /// <summary>
        /// 增加数据
        /// </summary>
        /// <param name="adopt"></param>
        /// <returns></returns>

        [HttpPost]
        public Result Add([FromBody] Pet_adopt adopt)
        {
            Result result = new Result();
            db.Pet_adopt.Add(adopt);
            result.Data = db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result Del(Guid id)
        {
            Result result = new Result();
            Pet_adopt? adopt = db.Pet_adopt.Where(w => w.ID == id).SingleOrDefault();
            if (adopt != null)
                db.Pet_adopt.Remove(adopt);
            result.Data = db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="adopt"></param>
        /// <returns></returns>
        [HttpPut]
        public Result Up([FromBody] Pet_adopt adopt)
        {
            Result result = new Result();
            db.Pet_adopt.Update(adopt);
            result.Data = db.SaveChanges();
            return result;
        }
    }
}
